﻿using System;
using Common;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using Model;

namespace DAL
{
    public class CodeMappingDAL : DB
    {
        public CodeMappingDAL(LoginInfo info)
        {
            base.loginInfo = info;
        } 
        /// <summary>
        /// 根据GUID字段获取CodeMapping实体对象
        /// </summary>
        /// <param name="guid">GUID</param>
        /// <returns>DictionaryInfo实体对象</returns>
        public CodeMapping GetInfoById(string guid)
        {
            HIDB.dbo cmd = base.conn();

            //添加输入输出参数
            IDataParameter[] sqlparams = { 
             new SqlParameter("@GUID",guid)    
            };

            //执行命令返回结果
            try
            {
                DataTable dt = cmd.GetDataTable("select * from Working_db.CodeMapping where GUID=@GUID", sqlparams);
                var model = new CodeMapping
                {
                    Guid = DBConvert.GetString(dt.Rows[0], "Guid"),
                    Name = DBConvert.GetString(dt.Rows[0], "Name"),
                    DotNetCode = DBConvert.GetString(dt.Rows[0], "DotNetCode"),
                    DBCode = DBConvert.GetString(dt.Rows[0], "DBCode"),
                    OrderNo = DBConvert.GetString(dt.Rows[0], "OrderNo"),
                    Description = DBConvert.GetString(dt.Rows[0], "Description")
                };
                return model;
            }
            catch (Exception ex)
            {
                Logger.LogError("GetInfoById", "GetInfoById", 0, 1, ex, "", "");
                return null;
            }
        }
        /// <summary>
        /// 更新CodeMapping表记录
        /// </summary>
        public int Update(SqlParameter[] pare)
        {
            int num = -1;

            HIDB.dbo cmd = base.conn();

            try
            {
                cmd.StoredUpdate("Working_db.UP_CodeMapping_Update", pare);
                num = 1;
            }
            catch (Exception ex)
            {
                Logger.LogError("UP_CodeMapping_Update", "UP_CodeMapping_Update", 0, 1, ex, "", "");
            }

            return num;
        }

        /// <summary>
        /// 根据实体获取DictionaryInfo实体List对象
        /// </summary>
        /// <param name="dictionaryInfo"> </param>
        /// <returns>DictionaryInfo实体对象</returns>
        public List<CodeMapping> GetList(IDataParameter[] Info)
        {
            var list = new List<CodeMapping>();

            var cmd = base.conn();

            try
            {
                DataTable dt = cmd.StoredDataTable("Working_db.UP_CodeMapping_GetList", Info);
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    var CodeMapping = new CodeMapping
                    {
                        Guid = DBConvert.GetString(dt.Rows[j], "Guid"),
                        Name = DBConvert.GetString(dt.Rows[j], "Name"),
                        DotNetCode = DBConvert.GetString(dt.Rows[j], "DotNetCode"),
                        DBCode = DBConvert.GetString(dt.Rows[j], "DBCode"),
                        Description = DBConvert.GetString(dt.Rows[j], "Description"),
                        OrderNo = DBConvert.GetString(dt.Rows[j], "OrderNo"),
                        Count = DBConvert.GetString(dt.Rows[j], "Count")
                    };
                    list.Add(CodeMapping);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError("UP_CodeMapping_GetList", "UP_CodeMapping_GetList", 0, 1, ex, "", "");
            }
            if (list != null)
            {
                return list;
            }
            return null;
        }
        public string Delete(string id)
        {
            HIDB.dbo cmd = base.conn();
            try
            {
                IDataParameter[] pare = { new SqlParameter("@Guid", id) };
                DataTable dt = cmd.StoredDataTable("Working_db.UP_CodeMapping_Delete", pare);
                if (dt != null && dt.Rows.Count > 0)
                {
                    return dt.Rows[0][0].ToString();
                }
                else
                    return "0";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public List<CodeMapping> GetMapping()
        {
            List<CodeMapping> result = new List<CodeMapping>();
            HIDB.dbo cmd = base.conn();
            try
            {
                DataTable dt = cmd.GetDataTable("Select Guid,Name from [Working_db].[CodeMapping] Order by OrderNo");
                if (dt != null && dt.Rows.Count > 0)
                {
                    CodeMapping _temp = null;
                    foreach (DataRow item in dt.Rows)
                    {
                        _temp = new CodeMapping() { Name = item["Name"].ToString(), Guid = item["Guid"].ToString() };
                        result.Add(_temp);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
    }
}
